/**
 * http://socket.io/
 */
var s = require('socket.io');
var socketList = [];
var io ;

function init( server ){
	io = s.listen( server );//.listen(8888);
	io.sockets.on('connection', function( socket ) {
		console.log( socketList.push( socket ), socket.id );
		socket.on('disconnect', function() {
		    console.log('Disconnect' );
		    sliceSkt( socket.id );
		});
	});
}

// 广播
function broadcast( evt , msg ){
	socketList.forEach( function( s ){
		s.emit( evt , msg );
	});
}

function sliceSkt( id ){
	for( var i = 0; i <  socketList.length; i++ ){
		if( socketList[i].id == id ){
			socketList.splice( i , 1);
			console.log('Deleted a socket.');
			break;
		}
	}
}

exports.init = init;
exports.broadcast = broadcast;